/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.serviceorder.web.ServiceOrderAction
   Module Description   :

   Date Created      : 2010/3/18
   Original Author   : cut7man
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.serviceorder.web;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.betwixt.io.BeanWriter;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.taobao.api.domain.Item;
import com.taobao.api.domain.PropValue;
import com.taobao.api.domain.Sku;
import com.yaodian100.core.admin.utils.AdminHelper;
import com.yaodian100.core.batch.service.BatchJobService;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.core.common.service.BaseManager;
import com.yaodian100.core.common.utils.JspUtil;
import com.yaodian100.core.common.utils.StringUtils;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.core.menu.service.MenuService;
import com.yaodian100.crm.workorder.service.WorkOrderService;
import com.yaodian100.ec.common.web.AbstractAction;
import com.yaodian100.ec.delivery.service.DeliveryRuleService;
import com.yaodian100.ec.member.entity.Member;
import com.yaodian100.ec.order.entity.OrderProduct;
import com.yaodian100.ec.order.entity.OrderProductDetail;
import com.yaodian100.ec.order.entity.Orders;
import com.yaodian100.ec.order.entity.ServiceOrderProduct;
import com.yaodian100.ec.order.entity.ServiceOrders;
import com.yaodian100.ec.order.service.CancelServiceOrder;
import com.yaodian100.ec.order.service.OrderService;
import com.yaodian100.ec.order.service.ServiceOrderService;
import com.yaodian100.ec.order.service.impl.ServiceOrderHelper;
import com.yaodian100.ec.product.entity.Product;
import com.yaodian100.ec.product.entity.SpecificationInventory;
import com.yaodian100.ec.product.entity.SpecificationMajor;
import com.yaodian100.ec.product.service.Product2Service;
import com.yaodian100.ec.tmall.service.TmallService;

/**
 * @author cut7man
 * 
 */
public class ServiceOrderAction extends AbstractAction {
	private String serviceId;
	private String orderId;
	private String iamlazy;
	private String message;
	private List<ServiceOrders> serviceOrderList;

	private Menu changeStatusMenu;
	private Menu returnStatusMenu;

	@Resource
	private BaseManager baseMgr;
	@Resource
	private ServiceOrderService serviceOrderService;
	private String exportCsv;
	@Resource
	private BatchJobService batchJobService;
	private Map<String, String> provincesMap;
	@Autowired
	private DeliveryRuleService deliveryRuleService;
	@Resource
	private WorkOrderService workOrderService;
	@Autowired
	private MenuService menuService;
	@Autowired
	private OrderService orderService;
	@Resource
	private Product2Service product2Service;
	@Resource
	private CancelServiceOrder cancelServiceOrder;

	@Autowired
	private TmallService tmallService;

	private List<List<SpecificationMajor>> prodMajors;
	private List<Map<String, SpecificationInventory>> prodInvMaps;

	/**
	 * @return the exportCsv
	 */
	public String getExportCsv() {
		return exportCsv;
	}

	/**
	 * @param exportCsv the exportCsv to set
	 */
	public void setExportCsv(String exportCsv) {
		this.exportCsv = exportCsv;
	}

	/**
	 * @return the iamlazy
	 */
	public String getIamlazy() {
		return iamlazy;
	}

	/**
	 * @param iamlazy the iamlazy to set
	 */
	public void setIamlazy(String iamlazy) {
		this.iamlazy = iamlazy;
	}

	private String AJAX;

	/**
	 * @return the aJAX
	 */
	public String getAJAX() {
		return AJAX;
	}

	/**
	 * @param aJAX the aJAX to set
	 */
	public void setAJAX(String aJAX) {
		AJAX = aJAX;
	}

	public String page() {
		if (AJAX != null && AJAX.equals("true")) {
			return iamlazy + "_ajax";
		} else {
			return iamlazy;
		}
	}

	private String returnPage(String orgPage) {
		if (AJAX != null && AJAX.equals("true")) {
			return orgPage + "_ajax";
		} else {
			return orgPage;
		}
	}

	public String confirmReturnProductOnDetail() {
		serviceOrderService.confirmReturnProduct(serviceId);
		domainId = workOrderService.closeOrderForReturnProductApply(serviceId, JspUtil.username());
		// return returnPage("confirmReturnProductOnDetail");
		return "workOrderList";
	}

	public String confirmReturnProduct() {
		serviceOrderService.confirmReturnProduct(serviceId);
		domainId = workOrderService.closeOrderForReturnProductApply(serviceId, JspUtil.username());
		// return returnPage("confirmReturnProduct");
		return "workOrderList";
	}

	public String batchConfirm() {
		if (ActionContext.getContext().getParameters().get("serviceId") instanceof String) {
			String serviceId = (String) ActionContext.getContext().getParameters().get("serviceId");
			serviceOrderService.confirmReturnProduct(serviceId);
			workOrderService.closeOrderForReturnProductApply(serviceId, JspUtil.username());
		} else {
			String[] serviceIds = (String[]) ActionContext.getContext().getParameters().get("serviceId");
			if (serviceId != null) {
				for (String serviceId : serviceIds) {
					serviceOrderService.confirmReturnProduct(serviceId);
					workOrderService.closeOrderForReturnProductApply(serviceId, JspUtil.username());
				}
			}
		}
		return returnPage("batchConfirm");
	}

	public String toReturnAllProduct() {
		if (StringUtils.isNotBlank(orderId)) {
			// 地址簿-省
			provincesMap = deliveryRuleService.getServiceOrderProvinces(orderId);
		}
		return "toReturnAllProduct";
	}

	public String export() {
		if (ActionContext.getContext().getParameters().get("serviceId") instanceof String) {
			String serviceId = (String) ActionContext.getContext().getParameters().get("serviceId");
			exportCsv = callOutCSVFormat(serviceId);
		} else {
			String[] serviceIds = (String[]) ActionContext.getContext().getParameters().get("serviceId");
			if (serviceIds != null) {
				StringBuffer sb = new StringBuffer("会员编号,会员帐号,退货编号\r\n");
				for (String serviceId : serviceIds) {
					sb.append(callOutCSVFormat(serviceId) + "\r\n");
				}
				exportCsv = sb.toString();
			}
		}
		return returnPage("export");
	}

	public String cancelReturnProduct() {
		serviceOrderService.cancelReturnProduct(serviceId);
		domainId = workOrderService.closeOrderForReturnProductApply(serviceId, JspUtil.username());
		// return returnPage("cancelReturnProduct");
		return "workOrderList";
	}

	public String cancelReturnProductOnDetail() {
		serviceOrderService.cancelReturnProduct(serviceId);
		domainId = workOrderService.closeOrderForReturnProductApply(serviceId, JspUtil.username());
		// return returnPage("cancelReturnProductOnDetail");
		return "workOrderList";
	}

	private String domainId;

	/**
	 * @return the domainId
	 */
	public String getDomainId() {
		return domainId;
	}

	/**
	 * @param domainId the domainId to set
	 */
	public void setDomainId(String domainId) {
		this.domainId = domainId;
	}

	private String callOutCSVFormat(String serviceId) {
		ServiceOrders so = (ServiceOrders) baseMgr.get(ServiceOrders.class, serviceId);
		Member member = (Member) baseMgr.get(Member.class, so.getMemberId());
		StringBuffer sb = new StringBuffer("");
		sb.append(so.getMemberId());
		sb.append(",");
		sb.append(member.getLoginId());
		sb.append(",");
		// sb.append(member.getMobile());
		sb.append(so.getServiceId());
		return sb.toString();
	}

	/**
	 * @return the serviceId
	 */
	public String getServiceId() {
		return serviceId;
	}

	/**
	 * @param serviceId the serviceId to set
	 */
	public void setServiceId(String serviceId) {
		this.serviceId = serviceId;
	}

	private String[] orderProdId;
	private String[] reason;
	private String[] accessaryDescription;
	private String[] description;

	private String receiverAddr;
	private String receiverArea;
	private String receiverCity;
	private String receiverExt;
	private String receiverMobile;
	private String receiverPhone;
	private String receiverPhoneArea;
	private String receiverProvince;
	private String receiverZip;
	private String receiverName;
	private String memberId;
	// 舊的換貨單號
	private String oldServiceId;
	private String orderDetailId;
	private List<OrderProductDetail> orderProductDetailList;

	public String returnAllProduct() {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		String host = request.getHeader("X-Forwarded-For");
		if (StringUtils.isBlank(host)) {
			host = StringUtils.defaultString(request.getRemoteAddr(), "0.0.0.0");
		} else if (host.length() > 100) {
			host = host.substring(0, 100);
		}
		message = serviceOrderService.returnAllProduct(orderId, memberId, orderProdId, accessaryDescription, reason,
				accessaryDescription, receiverName, receiverAddr, receiverArea, receiverCity, receiverExt, host,
				receiverMobile, receiverPhone, receiverPhoneArea, receiverProvince, receiverZip, "");
		return "returnAllProduct";

	}

	public String listServiceOrderByOrderProductAjax() {
		Long orderProdId = Long.valueOf(this.orderProdId[0]);
		List<ServiceOrders> serviceOrderListOfOneOrderProduct = this.serviceOrderService
				.getServiceOrderListByOrderProductId(orderProdId);
		this.serviceOrderList = serviceOrderListOfOneOrderProduct;

		// 相符資料只有一筆的時候，直接顯示該筆的明細
		if (this.serviceOrderList != null && this.serviceOrderList.size() == 1) {
			ServiceOrders serviceOrder = this.serviceOrderList.get(0);
			this.iamlazy = "detail";
			this.serviceId = serviceOrder.getServiceId();
			return this.page();
		}

		return "listServiceOrderByOrderProductAjax";
	}

	/**
	 * 依 order detail 查詢相符的 service order
	 */
	public String listServiceOrderByOrderDetailAjax() {
		Long orderDetailId = Long.valueOf(this.orderDetailId);
		List<ServiceOrders> serviceOrderListOfOneOrderDetail = this.serviceOrderService
				.getServiceOrderListByOrderDetailId(orderDetailId);
		this.serviceOrderList = serviceOrderListOfOneOrderDetail;

		// 相符資料只有一筆的時候，直接顯示該筆的明細
		if (this.serviceOrderList != null && this.serviceOrderList.size() == 1) {
			ServiceOrders serviceOrder = this.serviceOrderList.get(0);
			this.iamlazy = "detail";
			this.serviceId = serviceOrder.getServiceId();
			return this.page();
		}

		return "listServiceOrderByOrderProductAjax";
	}

	/**
	 * @return the orderId
	 */
	public String getOrderId() {
		return orderId;
	}

	/**
	 * @param orderId the orderId to set
	 */
	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * @return the reason
	 */
	public String[] getReason() {
		return reason;
	}

	/**
	 * @param reason the reason to set
	 */
	public void setReason(String[] reason) {
		this.reason = reason;
	}

	/**
	 * @return the accessaryDescription
	 */
	public String[] getAccessaryDescription() {
		return accessaryDescription;
	}

	/**
	 * @param accessaryDescription the accessaryDescription to set
	 */
	public void setAccessaryDescription(String[] accessaryDescription) {
		this.accessaryDescription = accessaryDescription;
	}

	/**
	 * @return the description
	 */
	public String[] getDescription() {
		return description;
	}

	/**
	 * @param description the description to set
	 */
	public void setDescription(String[] description) {
		this.description = description;
	}

	/**
	 * @return the receiverAddr
	 */
	public String getReceiverAddr() {
		return receiverAddr;
	}

	/**
	 * @param receiverAddr the receiverAddr to set
	 */
	public void setReceiverAddr(String receiverAddr) {
		this.receiverAddr = receiverAddr;
	}

	/**
	 * @return the receiverArea
	 */
	public String getReceiverArea() {
		return receiverArea;
	}

	/**
	 * @param receiverArea the receiverArea to set
	 */
	public void setReceiverArea(String receiverArea) {
		this.receiverArea = receiverArea;
	}

	/**
	 * @return the receiverCity
	 */
	public String getReceiverCity() {
		return receiverCity;
	}

	/**
	 * @param receiverCity the receiverCity to set
	 */
	public void setReceiverCity(String receiverCity) {
		this.receiverCity = receiverCity;
	}

	/**
	 * @return the receiverExt
	 */
	public String getReceiverExt() {
		return receiverExt;
	}

	/**
	 * @param receiverExt the receiverExt to set
	 */
	public void setReceiverExt(String receiverExt) {
		this.receiverExt = receiverExt;
	}

	/**
	 * @return the receiverMobile
	 */
	public String getReceiverMobile() {
		return receiverMobile;
	}

	/**
	 * @param receiverMobile the receiverMobile to set
	 */
	public void setReceiverMobile(String receiverMobile) {
		this.receiverMobile = receiverMobile;
	}

	/**
	 * @return the receiverPhone
	 */
	public String getReceiverPhone() {
		return receiverPhone;
	}

	/**
	 * @param receiverPhone the receiverPhone to set
	 */
	public void setReceiverPhone(String receiverPhone) {
		this.receiverPhone = receiverPhone;
	}

	/**
	 * @return the receiverPhoneArea
	 */
	public String getReceiverPhoneArea() {
		return receiverPhoneArea;
	}

	/**
	 * @param receiverPhoneArea the receiverPhoneArea to set
	 */
	public void setReceiverPhoneArea(String receiverPhoneArea) {
		this.receiverPhoneArea = receiverPhoneArea;
	}

	/**
	 * @return the receiverProvince
	 */
	public String getReceiverProvince() {
		return receiverProvince;
	}

	/**
	 * @param receiverProvince the receiverProvince to set
	 */
	public void setReceiverProvince(String receiverProvince) {
		this.receiverProvince = receiverProvince;
	}

	/**
	 * @return the receiverZip
	 */
	public String getReceiverZip() {
		return receiverZip;
	}

	/**
	 * @param receiverZip the receiverZip to set
	 */
	public void setReceiverZip(String receiverZip) {
		this.receiverZip = receiverZip;
	}

	/**
	 * @return the receiverName
	 */
	public String getReceiverName() {
		return receiverName;
	}

	/**
	 * @param receiverName the receiverName to set
	 */
	public void setReceiverName(String receiverName) {
		this.receiverName = receiverName;
	}

	/**
	 * @return the memberId
	 */
	public String getMemberId() {
		return memberId;
	}

	/**
	 * @param memberId the memberId to set
	 */
	public void setMemberId(String memberId) {
		this.memberId = memberId;
	}

	/**
	 * @return the orderProdId
	 */
	public String[] getOrderProdId() {
		return orderProdId;
	}

	/**
	 * @param orderProdId the orderProdId to set
	 */
	public void setOrderProdId(String[] orderProdId) {
		this.orderProdId = orderProdId;
	}

	public String toReturnProduct() {
		OrderProduct op = (OrderProduct) baseMgr.get(OrderProduct.class, Long.valueOf(orderProdId[0]));
		if (null != op) {
			// 地址簿-省
			provincesMap = deliveryRuleService.getServiceOrderProvinces(op.getOrderId());
		}
		// 讓 n 選 m 可以個自單獨退貨
		if (StringUtils.isNotBlank(this.orderDetailId)) {
			Long orderDetailIdLong = Long.valueOf(orderDetailId);
			OrderProductDetail orderProductDetailToReturn = this.orderService.getOrderProductDetail(orderDetailIdLong);
			List<OrderProductDetail> orderProductDetailList = new ArrayList<OrderProductDetail>();
			orderProductDetailList.add(orderProductDetailToReturn);
			this.orderProductDetailList = orderProductDetailList;
		}
		return "toReturnProduct";
	}

	public String toChangeProduct() {

		OrderProduct op = (OrderProduct) baseMgr.get(OrderProduct.class, Long.valueOf(orderProdId[0]));
		Orders order = op.getOrder();
		// 淘宝订单
		if ("yaodian100.tmall.com".equals(order.getDomainSrc())) {
			for (int i = 0; i < order.getOrderProduct().size(); i++) {
				OrderProduct theop = order.getOrderProduct().get(i);
				List<OrderProductDetail> opds = theop.getOrderProductDetail();
				for (OrderProductDetail opd : opds) {
					boolean isNeedSkip = false;
					if (tmallProducts != null && tmallProducts.size() > 0 && opd.getOuterProdId() != null
							&& opd.getOuterProdId().length() > 0) {
						for (Item item : tmallProducts) {
							if (item.getNumIid().longValue() == Long.valueOf(opd.getOuterProdId()).longValue()) {
								isNeedSkip = true;
								break;
							}
						}
					}
					if (opd.getOuterProdId() == null || opd.getOuterProdId().length() == 0) {
						isNeedSkip = true;
					}
					if ((theop.getProdTp().equals("0") || theop.getProdTp().equals("6")) && !isNeedSkip) {
						Item item = tmallService.getItem(Long.valueOf(opd.getOuterProdId()));
						tmallProducts.add(item);
						for (Sku sku : item.getSkus()) {
							List<PropValue> thePvs = tmallService.getSkuProps(opd.getOuterCid(), sku.getProperties());
							pvs.put(sku.getSkuId(), thePvs);
						}

					}
				}
			}
		}

		if (null != op) {
			// 地址簿-省
			provincesMap = deliveryRuleService.getServiceOrderProvinces(op.getOrderId());
		}

		// 雙屬性
		prodMajors = new ArrayList<List<SpecificationMajor>>();
		prodInvMaps = new ArrayList<Map<String, SpecificationInventory>>();
		Product product = (Product) baseMgr.get(Product.class, op.getProdId());
		for (OrderProductDetail opd : op.getOrderProductDetail()) {
			Product productD = (Product) baseMgr.get(Product.class, opd.getProdId());
			if (null != productD.getSpecMajors()) {
				List<SpecificationMajor> prodMajor = new ArrayList<SpecificationMajor>();
				for (SpecificationMajor major : productD.getSpecMajors()) {
					if ("N".equals(major.getIsDelete())) {
						prodMajor.add(major);
					}
				}
				Map<String, SpecificationInventory> prodInvMap = product2Service.getSpecificationInventoryMap(opd.getProdId());
				prodMajors.add(prodMajor);
				prodInvMaps.add(prodInvMap);
			}
		}

		return "toChangeProduct";
	}

	public String returnProduct() {
		Object[] results = baseMgr.queryByHql("from ServiceOrderProduct where orderProdId=?", new Object[] { Long
				.valueOf(orderProdId[0]) });
		ServiceOrderProduct sop = null;
		if (results != null && results.length > 0) {
			sop = (ServiceOrderProduct) results[0];
		}
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		String host = request.getHeader("X-Forwarded-For");
		if (StringUtils.isBlank(host)) {
			host = StringUtils.defaultString(request.getRemoteAddr(), "0.0.0.0");
		} else if (host.length() > 100) {
			host = host.substring(0, 100);
		}
		ServiceOrderHelper soh = serviceOrderService.returnProductByOrderProdId(orderProdId[0], memberId,
				accessaryDescription[0], paymentTp, receiverName, receiverAddr, receiverArea, receiverCity, receiverExt, host,
				receiverMobile, receiverPhone, receiverPhoneArea, receiverProvince, receiverZip, reason[0], description[0],
				String.valueOf(ServiceOrders.SERVICE_TYPE.RETURN.getValue()), orderId, "", prodSpecIds, qtys, "10",
				this.oldServiceId, this.orderDetailId);
		this.so = soh.getSo();
		this.so.setServiceOrderProducts(soh.getServiceOrderProducts());
		this.serviceOrderProducts = soh.getServiceOrderProducts();
		batchJobService.save(ServiceOrders.SERVICE_ORDER_BEAN.CREATE.getValue(), "<root>" + toXML(so) + "</root>", null, so
				.getServiceId());
		return "returnProduct";
	}

	public String changeProduct() {
		try {
			HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(
					ServletActionContext.HTTP_REQUEST);
			String host = request.getHeader("X-Forwarded-For");
			if (StringUtils.isBlank(host)) {
				host = StringUtils.defaultString(request.getRemoteAddr(), "0.0.0.0");
			} else if (host.length() > 100) {
				host = host.substring(0, 100);
			}
			ServiceOrderHelper soh = serviceOrderService.changeProductByOrderProdId(orderProdId[0], memberId,
					accessaryDescription[0], paymentTp, receiverName, receiverAddr, receiverArea, receiverCity, receiverExt,
					host, receiverMobile, receiverPhone, receiverPhoneArea, receiverProvince, receiverZip, reason[0],
					description[0], String.valueOf(ServiceOrders.SERVICE_TYPE.RETURN.getValue()), orderId, "", prodSpecIds,
					this.oldServiceId);
			this.so = soh.getSo();
			this.serviceOrderProducts = soh.getServiceOrderProducts();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "changeProduct";
	}

	private String paymentTp;
	private String[] prodSpecIds;
	private String[] qtys;

	/**
	 * @return the paymentTp
	 */
	public String getPaymentTp() {
		return paymentTp;
	}

	/**
	 * @param paymentTp the paymentTp to set
	 */
	public void setPaymentTp(String paymentTp) {
		this.paymentTp = paymentTp;
	}

	/**
	 * @return the prodSpecIds
	 */
	public String[] getProdSpecIds() {
		return prodSpecIds;
	}

	/**
	 * @param prodSpecIds the prodSpecIds to set
	 */
	public void setProdSpecIds(String[] prodSpecIds) {
		this.prodSpecIds = prodSpecIds;
	}

	/**
	 * @return the qtys
	 */
	public String[] getQtys() {
		return qtys;
	}

	/**
	 * @param qtys the qtys to set
	 */
	public void setQtys(String[] qtys) {
		this.qtys = qtys;
	}

	private ServiceOrders so;
	private Collection<ServiceOrderProduct> serviceOrderProducts;

	/**
	 * @return the so
	 */
	public ServiceOrders getSo() {
		return so;
	}

	/**
	 * @param so the so to set
	 */
	public void setSo(ServiceOrders so) {
		this.so = so;
	}

	/**
	 * @return the serviceOrderProducts
	 */
	public Collection<ServiceOrderProduct> getServiceOrderProducts() {
		return serviceOrderProducts;
	}

	/**
	 * @param serviceOrderProducts the serviceOrderProducts to set
	 */
	public void setServiceOrderProducts(Collection<ServiceOrderProduct> serviceOrderProducts) {
		this.serviceOrderProducts = serviceOrderProducts;
	}

	public String toReturnToChange() {
		return "toReturnToChange";
	}

	public String returnToChange() {
		try {
			HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(
					ServletActionContext.HTTP_REQUEST);
			String host = request.getHeader("X-Forwarded-For");
			if (StringUtils.isBlank(host)) {
				host = StringUtils.defaultString(request.getRemoteAddr(), "0.0.0.0");
			} else if (host.length() > 100) {
				host = host.substring(0, 100);
			}
			ServiceOrderHelper soh = serviceOrderService.changeProductByServiceId(serviceId, memberId,
					accessaryDescription[0], paymentTp, receiverName, receiverAddr, receiverArea, receiverCity, receiverExt,
					host, receiverMobile, receiverPhone, receiverPhoneArea, receiverProvince, receiverZip, reason[0],
					description[0], String.valueOf(ServiceOrders.SERVICE_TYPE.RETURN.getValue()), orderId, "", prodSpecIds);
			this.so = soh.getSo();
			this.serviceOrderProducts = soh.getServiceOrderProducts();
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		return "returnToChangeResult";
	}

	@Override
	public void prepare() throws Exception {
		super.prepare();
		clearActionErrors();
		if (changeStatusMenu == null) {
			changeStatusMenu = menuService.get("ServiceOrder.change.status");
		}
		if (returnStatusMenu == null) {
			returnStatusMenu = menuService.get("ServiceOrder.return.status");
		}
	}

	private String toXML(ServiceOrders serviceOrder) {

		String xml = null;
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			BeanWriter beanWriter = new BeanWriter(baos);
			beanWriter.enablePrettyPrint();
			beanWriter.write(serviceOrder);
			xml = baos.toString();
			beanWriter.flush();
			baos.flush();
			beanWriter.close();
			baos.close();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		return xml;

	}

	public Map<String, String> getProvincesMap() {
		return provincesMap;
	}

	public void setProvincesMap(Map<String, String> provincesMap) {
		this.provincesMap = provincesMap;
	}

	/**
	 * 舊的換貨單號
	 */
	public String getOldServiceId() {
		return this.oldServiceId;
	}

	public void setOldServiceId(String oldServiceId) {
		this.oldServiceId = oldServiceId;
	}

	public List<ServiceOrders> getServiceOrderList() {
		return this.serviceOrderList;
	}

	public void setServiceOrderList(List<ServiceOrders> serviceOrderList) {
		this.serviceOrderList = serviceOrderList;
	}

	public Menu getChangeStatusMenu() {
		return this.changeStatusMenu;
	}

	public void setChangeStatusMenu(Menu changeStatusMenu) {
		this.changeStatusMenu = changeStatusMenu;
	}

	public Menu getReturnStatusMenu() {
		return this.returnStatusMenu;
	}

	public void setReturnStatusMenu(Menu returnStatusMenu) {
		this.returnStatusMenu = returnStatusMenu;
	}

	public String getOrderDetailId() {
		return this.orderDetailId;
	}

	public void setOrderDetailId(String orderDetailId) {
		this.orderDetailId = orderDetailId;
	}

	public ServiceOrderService getServiceOrderService() {
		return this.serviceOrderService;
	}

	public void setServiceOrderService(ServiceOrderService serviceOrderService) {
		this.serviceOrderService = serviceOrderService;
	}

	public List<OrderProductDetail> getOrderProductDetailList() {
		return this.orderProductDetailList;
	}

	public void setOrderProductDetailList(List<OrderProductDetail> orderProductDetailList) {
		this.orderProductDetailList = orderProductDetailList;
	}

	public OrderService getOrderService() {
		return this.orderService;
	}

	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}

	public String revertCancel() {
		try {
			if (AdminHelper.hasPermission("GROUP_A-30-400-000")) {
				serviceOrderService.revertCancel(serviceId);
			} else {
				addActionError("权限不正确");
			}
		} catch (Exception e) {
			e.printStackTrace();
			addActionError(e.getMessage());
		}
		this.iamlazy = "detail";
		return this.page();
	}

	public String delServiceOrder() {
		try {
			if (AdminHelper.hasPermission("GROUP_A-30-400-000")) {
				if (StringUtils.isNotBlank(serviceId)) {
					ServiceOrders so = (ServiceOrders) baseMgr.get(ServiceOrders.class, serviceId);
					if (so != null) {
						if ("00".equals(so.getOrderSt())) {
							serviceOrderService.delete(so);
						} else if ("10".equals(so.getOrderSt())) {
							String result;
							result = cancelServiceOrder.cancelServiceOrder(serviceId, "客服取消");
							if (result.equals("true")) {
								serviceOrderService.delete(so);
							} else {
								addActionError(result);
							}
						} else {
							throw new CoreException("服务单以执行, 不可删除, 状态:" + so.getOrderSt());
						}
					}
				} else {
					addActionError("服务单号不正确：" + serviceId);
				}
			} else {
				addActionError("权限不正确");
			}
		} catch (Exception e) {
			e.printStackTrace();
			addActionError(e.getMessage());
		}
		this.iamlazy = "detail";
		return this.page();
	}

	/**
	 * @return the prodMajors
	 */
	public List<List<SpecificationMajor>> getProdMajors() {
		return prodMajors;
	}

	/**
	 * @param prodMajors the prodMajors to set
	 */
	public void setProdMajors(List<List<SpecificationMajor>> prodMajors) {
		this.prodMajors = prodMajors;
	}

	/**
	 * @return the prodInvMaps
	 */
	public List<Map<String, SpecificationInventory>> getProdInvMaps() {
		return prodInvMaps;
	}

	/**
	 * @param prodInvMaps the prodInvMaps to set
	 */
	public void setProdInvMaps(List<Map<String, SpecificationInventory>> prodInvMaps) {
		this.prodInvMaps = prodInvMaps;
	}

	private List<Item> tmallProducts = new ArrayList<Item>();
	private Map<Long, List<PropValue>> pvs = new HashMap<Long, List<PropValue>>();

	/**
	 * @return the tmallProducts
	 */
	public List<Item> getTmallProducts() {
		return tmallProducts;
	}

	/**
	 * @param tmallProducts the tmallProducts to set
	 */
	public void setTmallProducts(List<Item> tmallProducts) {
		this.tmallProducts = tmallProducts;
	}

	/**
	 * @return the pvs
	 */
	public Map<Long, List<PropValue>> getPvs() {
		return pvs;
	}

	/**
	 * @param pvs the pvs to set
	 */
	public void setPvs(Map<Long, List<PropValue>> pvs) {
		this.pvs = pvs;
	}

}
